﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FundRaising.BLL;
using FundRaising.DAL;
using FundRaising.DAL._FR_DSTableAdapters;
using System.Collections;


namespace FundRaising.GUI
{
    
    public partial class NewRequest : System.Web.UI.Page
    {
        public static DAL._FR_DS.AssoDomainDataTable   table;
        public static string FundName = null;
        public static string AssoName = null;
        public static string ProjName = null;
        public static string ReqYear = null;
        public static string Subproj = null;
        public static string Domain = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            GUI.EditRequest.first = true;
            GUI.EditRequest.first2 = true;
          if(GUI.EditRequest.j==0)
          {
              FillAssoNameDropDownList();
              FillFundNameDropDownList();
          }
        }

        

        public void FillAssoNameDropDownList()
        {
            RequestBLL bll = new RequestBLL();
            DAL._FR_DS_SmalParam.AssoDataTable AssoName = bll.GetAssoName();
            
            ArrayList Name = new ArrayList();
            //Hashtable Name = new Hashtable();
            Name.Add("");
            for (int i = 0; AssoName.Rows.Count > i; i++)
            {
                Name.Add(Convert.ToString(AssoName[i][0]));
            }
            DropDownList ddl = new DropDownList();
            DropDownList1.DataSource = Name;
            DropDownList1.DataTextField = null;
            DropDownList1.DataValueField = null;
            DropDownList1.DataBind();
            GUI.EditRequest.j = 1;
        }

            
        public void FillFundNameDropDownList()
        {
            RequestBLL bll = new RequestBLL();
            DAL._FR_DS_SmalParam.Fund2DataTable FundName = bll.GetFundName();
            ArrayList Name = new ArrayList();
            Name.Add("");
            for (int i = 0; FundName.Rows.Count > i; i++)
            {
                Name.Add(Convert.ToString(FundName[i][0]));
            }
            DropDownList ddl = new DropDownList();
            DropDownList4.DataSource = Name;
            DropDownList4.DataTextField=null ;
            DropDownList4.DataValueField = null;
            DropDownList4.DataBind();
            GUI.EditRequest.j = 1;
        }


        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            AssoDomainBLL BLL = new AssoDomainBLL();
            DAL._FR_DS.AssoDomainDataTable table;    
            table = BLL.GetSubProject(DropDownList2.SelectedValue);

            ArrayList Subproj = new ArrayList();
            for (int i = 0; table.Rows.Count > i; i++)
            {          
                Subproj.Add(Convert.ToString(table[i][4]));
            }
            DropDownList ddl = new DropDownList();
            ddl = (DropDownList)DetailsView1.FindControl("DropDownList3");
            ddl.DataSource = Subproj;
            ddl.DataBind();
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DetailsView1.Visible = true;
        }


        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            DomainFundBLL BLL = new DomainFundBLL();
            DAL._FR_DS.DomainFundDataTable table;
            table = BLL.GetDomainByFund(DropDownList4.SelectedValue);

            ArrayList Domain = new ArrayList();
            for (int i = 0; table.Rows.Count > i; i++)
            {
                Domain.Add(Convert.ToString(table[i][1]));
            }
            DropDownList ddl = new DropDownList();
            ddl = (DropDownList)DetailsView1.FindControl("DropDownList5");
            ddl.DataSource = Domain;
            ddl.DataBind();
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            DropDownList SubDDL = new DropDownList();
            DropDownList DomDDL = new DropDownList();
            SubDDL = (DropDownList)DetailsView1.FindControl("DropDownList3");
            DomDDL = (DropDownList)DetailsView1.FindControl("DropDownList5");
            GUI.NewRequest.FundName =   DropDownList4.SelectedValue;
            GUI.NewRequest.AssoName =   DropDownList1.SelectedValue;
            GUI.NewRequest.ProjName =   DropDownList2.SelectedValue;
            GUI.NewRequest.ReqYear = DropDownList6.SelectedValue;
            GUI.NewRequest.Subproj = SubDDL.SelectedValue;
            GUI.NewRequest.Domain = DomDDL.SelectedValue;

            
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditRequest.aspx");
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            RequestBLL bll = new RequestBLL();
            string test = DropDownList1.SelectedValue;
            DAL._FR_DS_SmalParam.AssoDomainDataTable  project = bll.GetProjByAsso(DropDownList1.SelectedValue);
            ArrayList Name = new ArrayList();
            //Hashtable Name = new Hashtable();
            Name.Add("");
            for (int i = 0; project.Rows.Count > i; i++)
            {
                Name.Add(Convert.ToString(project[i][0]));
            }
            DropDownList ddl = new DropDownList();
            DropDownList2.DataSource = Name;
            DropDownList2.DataTextField = null;
            DropDownList2.DataValueField = null;
            DropDownList2.DataBind();
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('בקשה התבצעה בהצלחה')</script>";
            Page.Controls.Add(lbl);
            //Response.Redirect("EditRequest.aspx");
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
          
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
              DropDownList SubbDDL = new DropDownList();
              SubbDDL = (DropDownList)DetailsView1.FindControl("DropDownList3");
              AssoDomainBLL bll = new AssoDomainBLL();

              table = bll.getdomainbyothers(DropDownList1.SelectedValue, DropDownList2.SelectedValue, SubbDDL.SelectedValue);
              
                

             
        }

        protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList SubbDDL = new DropDownList();
            SubbDDL = (DropDownList)DetailsView1.FindControl("DropDownList3");
            AssoDomainBLL bll = new AssoDomainBLL();
            DropDownList domDDL = new DropDownList();
            domDDL = (DropDownList)DetailsView1.FindControl("DropDownList5");
            string test = domDDL.SelectedValue;
            bool check = false;
            if (table == null) table = bll.getdomainbyothers(DropDownList1.SelectedValue, DropDownList2.SelectedValue, SubbDDL.SelectedValue);
            for (int i = 0; table.Rows.Count > i; i++)
            {
                string dom = Convert.ToString(table[i][1]);
                if (dom.Equals(test)) check = true; 
            }
            if (!check) MessageBox("הערה : תחום פעילות הנבחר אינו  תואם לתחום הפעילות המשוייך לעמותה  ");
        }

        private void MessageBox(string msg)
        {
            System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }






    }
}
